package cn.imvc.app.models;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.ArticleClass;
import cn.imvc.app.repositories.ArticleClassRepository;
import cn.imvc.data.domain.ViewRequest;

@Service
public class ArticleClassModelImpl implements ArticleClassModel {

	@Autowired
	private ArticleClassRepository articleClassRepository;

	@Autowired
	MemberModel memberModel;

	public ArticleClass get(int id) {
		return articleClassRepository.findOne(id);
	}

	public Page<ArticleClass> find(ViewRequest viewRequest) {
		return (Page<ArticleClass>) articleClassRepository.findAll();
	}

	public ArticleClass save(ArticleClass articleClass) {
		return  articleClassRepository.saveAndFlush(articleClass);
	}

	public ArticleClass update(ArticleClass newArticleClass) {
		ArticleClass loadArticleClass = get(newArticleClass.getId());
		BeanUtils.copyProperties(newArticleClass, loadArticleClass, new String[] { "id" });
		return articleClassRepository.saveAndFlush(loadArticleClass);
	}

	public void delete(int id) {
		articleClassRepository.delete(id);
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			delete(id);
		}
	}
	
	public List<Object[]> findMaxId(){
		return articleClassRepository.findAllSql("select max(id) from article_class"); 
	}
}
